import tornado.web,time,json
from myredis import r
import tornado.ioloop
from utlis.my_jwt import jj

class  BaseHandler(tornado.web.RequestHandler):
    # 重写该方法
    def set_default_headers(self):
        self.set_header("Access-Control-Allow-Origin", "*") # 允许所有的域名访问
        self.set_header("Access-Control-Allow-Headers", "*") # 允许携带所有的参数
        self.set_header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE") # 允许所有的请求方式
        self.set_header("Access-Control-Max-Age", "3600") # 允许连接时，最大响应时间
    # 定义一个响应的方法，不需要实现什么功能
    def options(self):
        pass

    # def prepare(self):
    #     url = self.request.uri
    #     whitelist = ['/login/',]
    #     if url not in whitelist:
    #         #  获取token
    #         headers = self.request.headers
    #         try:
    #             token = headers['token']
    #             flag = jj.check(token)
    #             if flag == True:
    #                 # 解析验证时间
    #                 payload = jj.jwt_decode(token)
    #                 ptime = payload['data']['exp']
    #                 now = time.time()
    #                 if int(now) - int(ptime) > 3600:
    #                     self.finish({"code": 402})
    #                     return
    #                 key="logoutlist"
    #                 res=r.sortedset_getall(key)
    #                 for i in res:
    #                     if token == i.decode():
    #                         self.finish({"code":"已过期，小崽子别访问接口了"})
    #                 # 判断此用户是否有权限访问
    #                 key = "interlist" + str(payload['data']['roleid'])
    #                 interlist = json.loads(r.get_str(key))
    #                 print("interlist>>>>>>>>", interlist)
    #                 if url not in interlist:
    #                     self.finish({"code": 408})
    #                     return
    #             else:
    #                 self.finish({"code": 403})
    #                 return
    #         except:
    #             self.finish({"code": 401})
    #             return